
package com.shop.cereshop.app.service.nearby.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.nearby.BuyerNearbyImageDAO;
import com.shop.cereshop.app.page.seckill.SeckillProductProblem;
import com.shop.cereshop.app.service.nearby.BuyerNearbyImageService;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.nearby.BuyerNearbyImage;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 买家附近的人图片库（仅图片，无相册分类）
 * </p>
 *
 * @author wang.yao
 * @date 2025-09-25
 */
@Slf4j
@Service
public class BuyerNearbyImageServiceImpl implements BuyerNearbyImageService {
    @Resource
    private BuyerNearbyImageDAO buyerNearbyImageDAO;

    @Override
    public Long save(BuyerNearbyImage entity) {
        buyerNearbyImageDAO.insert(entity);
        return entity.getId();
    }

    @Override
    public boolean saveBatch(Long buyerUserId, List<BuyerNearbyImage> entities) {
        QueryWrapper<BuyerNearbyImage> wrapper = new QueryWrapper<BuyerNearbyImage>()
                .eq("buyer_user_id", buyerUserId);
        buyerNearbyImageDAO.delete(wrapper);
        if (CollectionUtils.isEmpty(entities)) {
            return true;
        }
        for (BuyerNearbyImage e : entities) {
            buyerNearbyImageDAO.insert(e);
        }
        return true;
    }

    @Override
    public Integer update(BuyerNearbyImage entity, Long operatorUserId) {
        // 仅允许本人更新
        return buyerNearbyImageDAO.update(entity, new QueryWrapper<BuyerNearbyImage>()
                .eq("id", entity.getId())
                .eq("buyer_user_id", operatorUserId)
                .eq("is_deleted", 0));
    }

    @Override
    public Integer delete(Long id, Long operatorUserId) {
        // 仅允许本人删除，逻辑删除
        BuyerNearbyImage upd = new BuyerNearbyImage();
        upd.setIsDeleted(true);
        return buyerNearbyImageDAO.update(upd, new QueryWrapper<BuyerNearbyImage>()
                .eq("id", id)
                .eq("buyer_user_id", operatorUserId)
                .eq("is_deleted", 0));
    }

    @Override
    public BuyerNearbyImage getById(Long id) {
        return buyerNearbyImageDAO.selectOne(
                new QueryWrapper<BuyerNearbyImage>()
                        .eq("id", id)
                        .eq("is_deleted", 0)
        );
    }

    @Override
    public Page<BuyerNearbyImage> listByUser(Long buyerUserId, Long operatorUserId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        QueryWrapper<BuyerNearbyImage> wrapper = new QueryWrapper<BuyerNearbyImage>()
                .eq("buyer_user_id", buyerUserId)
                .eq("is_deleted", 0)
                .orderByDesc("sort_order");
        // 若目标用户不是本人，仅查询已审核通过的图片（status=1）
        if (operatorUserId == null || !buyerUserId.equals(operatorUserId)) {
            wrapper.eq("status", 1);
        }
        List<BuyerNearbyImage> list = buyerNearbyImageDAO.selectList(wrapper);
        PageInfo<BuyerNearbyImage> pageInfo = new PageInfo<>(list);
        return new Page(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public List<BuyerNearbyImage> listByUserIds(List<String> allIds) {
        QueryWrapper<BuyerNearbyImage> wrapper = new QueryWrapper<BuyerNearbyImage>()
                .in("buyer_user_id", allIds)
                .eq("status", 1)
                .select("id","buyer_user_id", "image_url", "description", "sort_order");
        return buyerNearbyImageDAO.selectList(wrapper);
    }
}
